/*
 * @Description: 
 * @Author: 韩双双
 * @Date: 2021-04-20 19:01:56
 * @LastEditTime: 2021-05-06 00:29:27
 * @LastEditors: 邢
 */
import { getMoreList ,getAllrecommend, getArticletag, getArticlecategory, getArticleallrecommend, getarticleDetail, getCategoryDetail, gettagGit, getsearch } from '@/seivers/article'
 import { makeAutoObservable } from 'mobx';
 import { Iarticleloding, Icategory, ICategoryDetail, IHomeList, IrecommendSwiper, Isearch, ITaggit } from '@/utils/interface/Homepage'


 class Articel {

     constructor(){
        makeAutoObservable(this)
     }
     articleRecommend:IHomeList[] = [];
     articleloding:Iarticleloding[] = [];
     articleTag:Icategory[] = [];
     category:Icategory[] = [];
     recommendSwiper: IrecommendSwiper[] = []
 
    CategoryDetail:ICategoryDetail[]  = []
    tagGitDetail:ITaggit[]=[]
    search:Isearch[]=[]
    //请求更多的列表
    page:number = 1
    pageSize:number = 12;
    total:number = 0;
    isFlag:boolean = false;//用于锁住请求。只有下次请求开始才会打开锁

     //请求 dl dd
     async getArticleRecommend(page?:number,pageSize?:number,status?:string){
         //判断锁是否为打开状态
         if(this.isFlag){
             return ;
         }
         //当数据请求完之后不在请求
         if(this.total && (this.page-1) * this.pageSize >= this.total){
             return;
         }
         this.isFlag = true;
         //数据没有请求结束，则继续请求
        let result = await getMoreList(page,pageSize,status);

        if(result.statusCode === 200){
            //当加载第二个页面以及之后的页面，将内容添加进之前的数据里面而不是替换
            if(this.page>1){
                this.articleRecommend = [...this.articleRecommend,...result.data[0]]
            }else{
                this.articleRecommend = result.data[0]
            }
            this.total = result.data[1]
            this.page++
            this.isFlag = false
        }
    }
    async getMoreList(){
        let result = await getMoreList(this.page,this.pageSize);
        if(result.statusCode === 200){
            this.articleRecommend = result.data[0]
            this.page++
        }
    }
    //列表
    async getAllrecommend(){
        let result = await getAllrecommend();
        // console.log(result)
        if(result.statusCode === 200){
            this.articleloding = result.data
        }
    }
    //标签
    async getArticletag (){
        let result = await getArticletag()
        // console.log(result)
        if(result.statusCode === 200){
            this.articleTag = result.data
        }
    }
    //分类
    async getArticlecategory(){
        let result = await getArticlecategory();
        // console.log(result)
        this.category = result.data
    }
    //轮播图
    async getArticleallrecommend(){
        let result = await getArticleallrecommend();
        this.recommendSwiper  = result.data
    }
    //点击列表
    async getarticleDetail(ID:string){
        let res = await getarticleDetail(ID);
        console.log(res)
    }
    //点击文章分类
    async getCategoryDetail(ID:string){
        // console.log(234124)
        let res = await getCategoryDetail(ID);
        // console.log(res)
        if(res.statusCode === 200){
            // console.log(JSON.parse(JSON.stringify(res.data[0])))
            this.CategoryDetail =res.data[0]
        }
        
    }
    //点击文章标签
    async gettagGit(ID:string){
        // console.log(234124)
        let res = await gettagGit(ID);
        console.log(res)
        if(res.statusCode === 200){
            // console.log(JSON.parse(JSON.stringify(res.data[0])))
            this.tagGitDetail =res.data[0]
        }
    }
    //点击搜所
    async getsearch(keyword:{}){
        let res = await getsearch(keyword);
        console.log(res)
        if(res.statusCode === 200){
           this.search =res.data
        }
    }
    //上拉加载更多
    // async getMoreList(){
    //     let result = await getMoreList(this.page,this.pageSize);
    //     if(result.statusCode === 200){
    //         console.log(result.data)
    //         this.articleRecommend = result.data[0]
    //     }
    // }
 }
 export default Articel
